/**
 * 
 */
package anvil;

import java.util.ArrayList;
import java.util.concurrent.Callable;
import myexception.AuthException;
import myexception.NetWorkException;
import myexception.OtherException;
import myexception.OutRateException;

import weibo4j.Timeline;
import weibo4j.model.Paging;
import weibo4j.model.Status;
import weibo4j.model.StatusWapper;
import weibo4j.model.WeiboException;

/**
 * @author liutao
 * 
 */
public class Splint implements Callable<Integer> {
	private String authcode = "";// @Documented authurication code
	private String thid = "";// @Documented thread for file name
	private ArrayList<String> thseeds = new ArrayList<String>();// @Documented
																// seeds for
																// each thread
	private ArchiveLoader ar;

	public Splint(String authcode, String thid, ArrayList<String> thseeds) {
		super();
		this.authcode = authcode;
		this.thid = thid;
		this.thseeds = thseeds;
		this.ar = new ArchiveLoader(this.thid);
	}

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		String testcode = "2.00jz81DCueJwvCa178279802ieORTE";
		ArrayList<String> seeds = new ArrayList<String>();
		seeds.add("1881468847");
		seeds.add("1881468847");
		Splint s = new Splint(testcode, "testid", seeds);
		s.call();

	}

	public void testShow(StatusWapper sw) {
		int i = 0;
		for (Status s : sw.getStatuses()) {
			System.out.println(i++ + "\t" + s.getCreatedAt().toString() + "\t"
					+ s.getText());
		}
	}

	@Override
	public Integer call() throws Exception {
		// TODO Auto-generated method stub
		// @Documented thread modular , time-username-content
		System.out.println(this.thid + "-" + this.authcode + "-"
				+ this.thseeds.size());
		int count = 0;
		Timeline tl = new Timeline();
		
		 tl.client.setToken(authcode);
		this.ar.Init();// @Documented create the file
		// Iterator<String> i = thseeds.iterator();
		int i = 0;
		for (i = 0; i < thseeds.size(); i++) {
			String id = thseeds.get(i);
			StatusWapper sw = null;
			try {
				Paging pg = new Paging(1, 100);// @Documented page 1, 20items
												// per
												// page
				try {
					sw = tl.getUserTimelineByUid(id, pg, 0, 0);
					this.testShow(sw);// @Debug show weibos
					this.ar.sw = sw;// @Documented load the wrapper

					this.ar.Save();//

					count += sw.getStatuses().size();
				} catch (WeiboException e) {// TODO check error code
					i--;
					if (e.getStatusCode() == 0) {
						throw new AuthException();
					} else if (e.getStatusCode() == 10010) {
						throw new NetWorkException();
					} else if (e.getStatusCode() == 10023) {
						throw new OutRateException();
					} else {
						throw new OtherException(Integer.toString(e
								.getStatusCode()) + e.getMessage());
					}
				} catch (Exception e) {
					i--;
					throw new OtherException(e.getMessage());
				}
			} catch (AuthException ae) {
				ae.printStackTrace();
				System.out.println("Please Run Auto-Auth modular");
				// TimeUnit.MINUTES.sleep(30);
				Thread.sleep(1000 * 60 * 10);

			} catch (NetWorkException nwe) {
				nwe.printStackTrace();
				System.out.println("Please Check Connection");
				// TimeUnit.MINUTES.sleep(30);
				Thread.sleep(1000 * 60 * 10);

			} catch (OutRateException ore) {
				ore.printStackTrace();
				System.out.println("Out Of Rate");
				// TimeUnit.MINUTES.sleep(30);
				Thread.sleep(1000 * 60 * 10);
			} catch (OtherException oe) {
				oe.printStackTrace();
				System.out.println("Unknow Exception" + oe.getMessage());
				// TimeUnit.MINUTES.sleep(30);
				Thread.sleep(1000 * 60 * 10);
			}
		}
		this.ar.Suspend();
		return count;
	}
}
